146D - Lucky Number 2 - CodeForces Solution


brute force constructive algorithms implementation *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define PG_Mazen ios_base::sync_with_stdio( 0 ), cin.tie( 0 ), cout.tie( 0 );
#define el '\n'
#define sp ' '
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define SZ(v) (v).size()
#define pb(n) push_back(n)
#define pf(n) push_front(n)
#define eb(n, m) emplace_back(n, m)
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define int long long
#define double long long double
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll,ll>
#define F first
#define S second
#define loop int t; cin >> t; for(int i=1;i<=t;++i)
#define f0(i, st, n) for(int i=st;i<n;++i)
#define f1(i, st, n) for(int i=st;i<=n;++i)
#define fe(it, n) for(auto &it : n)
#define mms(arr, val) memset(arr, val, sizeof arr)
#define mmv(vec, val) memset(vec[0], val, sizeof(vec) * SZ(vec))
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

const int N = 1e5 + 5, MOD = 2019, oo = 0x3f3f3f3f;
const ll ooo = 0x3f3f3f3f3f3f3f3f;

void testCase() {
    int a1, a2, a3, a4;
    cin >> a1 >> a2 >> a3 >> a4;
    if (abs(a3 - a4) > 1 || a1 < max(a3, a4) || a2 < max(a3, a4) || (a1 + a2 <= a3 + a4)) {
        cout << "-1";
        return;
    }

    deque<char> dq;
    while (a3 + a4 > 1) {
        dq.pb('4');
        dq.pb('7');
        --a1, --a2, --a3, --a4;
    }
    if (a1)
        dq.pb('4'), --a1;
    else
        dq.pf('7'), --a2;
    if (a3) {
        if (dq.front() == '7' && a1)
            dq.pf('4'), --a1;
        else if(dq.front() == '4' && a2)
            dq.pb('7'), --a2;
        else {
            cout << "-1";
            return;
        }
    }
    if (a4) {
        if (dq.front() == '4' && a2)
            dq.pf('7'), --a2;
        else if(dq.front() == '7' && a1)
            dq.pb('4'), --a1;
        else {
            cout << "-1";
            return;
        }
    }
    if (a1) {
        if (dq.front() == '4')
            while (a1--) dq.pf('4');
        else {
            dq.pop_front();
            while (a1--) dq.pf('4');
            dq.pf('7');
        }
    }
    if (a2) {
        if (dq.back() == '7')
            while (a2--) dq.pb('7');
        else {
            dq.pop_back();
            while (a2--) dq.pb('7');
            dq.pb('4');
        }
    }
    fe(it, dq) cout << it;
}

int32_t main() {
    PG_Mazen
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    testCase();
}
	 	 		  				 				 		 	    			 	


Comments

Submit
0 Comments
More Questions

162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted